'use strict'
/**
 * 合同通用操作服务类
 */
define(function (require, exports, module) {
    var angular = require('angular');
    var util = require("util");
    var api_prefix = G_CONFIG["api_prefix"]; //api前缀

    var ContractCommon = angular.module('ContractCommonService', []);

    ContractCommon.service('contractCommonService', ['$rootScope', function ($rootScope) {
        var service = {
            getContract: function (contractId, callback) {
                /**
                 * 获取合同信息
                 */
                var get_data = {
                    contractId: contractId
                };
                var url = api_prefix + "/contract/get-contract-info";
                this.ajax('合同信息', url, get_data, callback, 'GET', false);
            },
            getContractSuppliers: function(callback) {
                /**
                 * 获取供应商列表
                 * @type {{}}
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-suppliers";
                this.ajax('供应商列表', url, get_data, callback, 'GET', false);
            },
            getContractTypes: function (callback) {
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-types";
                this.ajax('供应商列表', url, get_data, callback, 'GET', false);
            },
            getSupplierInfoByName: function(supplier_name, callback) {
                var get_data = {supplier_name: supplier_name};
                var url = api_prefix + "/contract/get-supplier-info";
                this.ajax('获取供应商id', url, get_data, callback, 'GET', false);
            },
            getBusinessTypeIdByName: function (business_type, callback) {
                var get_data = {business_type: business_type};
                var url = api_prefix + "/contract/get-business-type-info";
                this.ajax('获取供应商id', url, get_data, callback, 'GET', false);
            },
            ExportSupplierPaymentModel: function(callback) {
                var get_data = {};
                var url = api_prefix + "/contract/export-supplier-payment-model";
                this.ajax('导出', url, get_data, callback, 'GET', false);
            },
            getBusinessProduct: function(businessTypeName, callback) {
                var get_data = {businessTypeName: businessTypeName};
                var url = api_prefix + "/contract/get-product-type";
                this.ajax('产品类型', url, get_data, callback, 'GET', false);
            },
            getBusinessTypeAll: function (callback) {
                var get_data = {};
                var url = api_prefix + "/contract/get-business-type";
                this.ajax('业务类型',url,get_data,callback,'GET',false);
            },
            getContractNatures: function (callback) {
                /**
                 * 获取合同性质列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-natures";
                this.ajax('合同性质', url, get_data, callback, 'GET', false);
            },
            getContractCategories: function (callback) {
                /**
                 * 获取合同类别列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-categories";
                this.ajax('合同类别', url, get_data, callback, 'GET', false);
            },
            getContractBigClass: function (callback) {
                /**
                 * 获取合同大类列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-big-class";
                this.ajax('合同大类', url, get_data, callback, 'GET', false);
            },
            getContractSmallClass: function (bigClassId, callback) {
                /**
                 * 获取合同小类列表
                 */
                var get_data = {bigClassId: bigClassId};
                var url = api_prefix + "/contract/get-contract-small-class";
                this.ajax('合同小类', url, get_data, callback);
            },
            getContractAreas: function (callback) {
                /**
                 * 获取合同区域列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-contract-area-list";
                this.ajax('合同区域', url, get_data, callback);
            },
            getCompanies: function (callback) {
                /**
                 * 获取公司列表
                 */
                var get_data = {};
                var url = api_prefix + "/erp-interface/get-companies";
                this.ajax('公司', url, get_data, callback);
            },
            getProducts: function (callback) {
                /**
                 * 获取产品列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-products";
                this.ajax('产品', url, get_data, callback);
            },
            getBussAccountTypes: function (callback) {
                /**
                 * 获取核算收入类别列表
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-buss-account-types";
                this.ajax('核算收入类别', url, get_data, callback, 'GET', false);
            },
            getContractRevenue: function (contractId, callback) {
                /**
                 * 获取收入信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/contract/get-contract-revenue";
                this.ajax('收入信息', url, get_data, callback, 'POST');
            },
            getContractRevenueById: function (oid, callback) {
                /**
                 * 获取单条收入信息
                 */
                var get_data = {oid: oid};
                var url = api_prefix + "/contract/get-contract-revenue-by-id";
                this.ajax('收入信息', url, get_data, callback, 'POST');
            },
            getContractRevenueCarryoverById: function (oid, callback) {
                /**
                 * 获取单条收入结转信息
                 */
                var get_data = {oid: oid};
                var url = api_prefix + "/contract/get-contract-carryover-by-id";
                this.ajax('结转信息', url, get_data, callback, 'POST');
            },
            getContractOrganizationInfo: function (contractId, callback) {
                /**
                 * 获取开通信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/contract/get-contract-organization-info";
                this.ajax('开通信息', url, get_data, callback, 'POST');
            },
            getContractBalanceInfo: function (contractId, callback) {
                /**
                 * 获取结算与返款信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/contract/get-contract-balance-info";
                this.ajax('结算返款信息', url, get_data, callback, 'POST');
            },
            getContractUnifiedInfo: function (contractId, callback) {
                /**
                 * 获取合同应收统一信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-contract-unified-info";
                this.ajax('合同应收信息', url, get_data, callback, 'GET');
            },
            getPayInfoByContractId: function (contractId, callback) {
                /**
                 * 获取合同应收统一信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-pay-info-by-contract-id";
                this.ajax('合同应收信息', url, get_data, callback, 'GET');
            },
            getSuppliersPaymentByContractId: function (contractId, callback) {
                /**
                 * 获取合同应收统一信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-suppliers-payment-by-contract-id";
                this.ajax('合同应收信息', url, get_data, callback, 'GET');
            },
            getRelationIncome: function (contractId, callback) {
                /**
                 * 获取关联收入项信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/contract/get-relation-income";
                this.ajax('关联收入项', url, get_data, callback, 'GET');
            },
            genContractFlowCode: function (areaCode, customerId, contractCode, callback) {
                /**
                 * 生成合同编号
                 */
                var get_data = {areaCode: areaCode, customerId: customerId, contractCode: contractCode};
                var url = api_prefix + "/contract/get-contract-flow-code";
                this.ajax('合同编号', url, get_data, callback, 'GET');
            },
            getContractReceivable: function (contractId, callback) {
                /**
                 * 根据合同ID获取对应的应收款信息
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-contract-receivable";
                this.ajax('应收款信息', url, get_data, callback, 'GET');
            },
            getReceivableFundName: function (callback) {
                /**
                 * 获取应收款名称
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-receivable-fund-name";
                this.ajax('应收款名称信息', url, get_data, callback, 'GET', false);
            },
            getDisabledInvoice: function( payment_id, callback ){
                /**
                 * 获取应收信息->编辑发票时，发票的默认信息
                 */
                var get_data = { payment_id: payment_id };
                var url = api_prefix + "/receivable/get-contract-payment";
                this.ajax('编辑回款默认信息', url, get_data, callback, 'GET', false);
            },
            getInvoiceReceivableInfoByContract: function (contractId, callback) {
                /**
                 * 获取发票与应收款对应关系
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-invoice-receivable-info-by-contract";
                this.ajax('发票与应收款对应关系', url, get_data, callback, 'GET', false);
            },
            getCanUseInvoiceInfo: function (contractId,keyword, callback) {

                /**
                 * 获取未关联的发票
                 */
                var get_data = {contractId: contractId,keyword : keyword};
                var url = api_prefix + "/receivable/get-can-use-invoice-info";
                this.ajax('未关联的发票', url, get_data, callback, 'GET', true);
            },
            getContractPaymentHaveAmountByContract: function (contractId, callback) {
                /**
                 * 获取还未完全开完发票的回款记录
                 */
                var get_data = {contractId: contractId};
                var url = api_prefix + "/receivable/get-contract-payment-have-amount-by-contract";
                this.ajax('未完全开完发票的回款记录', url, get_data, callback, 'GET', true);
            },
            actionGetContractAccountCompanyList: function (propertyId, callback) {
                /**
                 * 根据性质id,获取帐套公司列表
                 */
                var get_data = {propertyId: propertyId};
                var url = api_prefix + "/contract/get-contract-account-company-list";
                this.ajax('账套公司列表', url, get_data, callback, 'GET', false);
            },
            actionGetContractCompanyList: function (propertyId, callback) {
                /**
                 * 根据性质id,获取签署公司列表
                 */
                var get_data = {propertyId: propertyId};
                var url = api_prefix + "/contract/get-contract-company-list";
                this.ajax('签署公司列表', url, get_data, callback, 'GET', false);
            },
            getFundSmallClass: function (propertyId, callback) {
                /**
                 * 获取款项小类
                 */
                var get_data = {propertyId: propertyId};
                var url = api_prefix + "/contract/get-fund-small-class";
                this.ajax('款项小类', url, get_data, callback, 'GET', false);
            },
            getInvoiceBusinessUnit: function (callback) {
                /**
                 * 获取开票单位
                 */
                var get_data = {keyword: ''};
                var url = api_prefix + "/invoice/get-invoice-business-unit";
                this.ajax('开票单位', url, get_data, callback, 'GET', false);
            },
            getAccountHkList: function (accountName, callback, failedBack) {
                /**
                 * 取财务系统的回款信息
                 */
                var get_data = {paymentAccountName: accountName};
                var url = api_prefix + "/erp-interface/get-account-hk-list";
                this.ajax('财务系统的回款信息', url, get_data, callback, 'GET', true, failedBack);
            },
            getWithdrawFeeRate: function (callback) {
                /**
                 * 取汇率比例
                 */
                var get_data = {};
                var url = api_prefix + "/contract/get-withdraw-fee-rate";
                this.ajax('取汇率比例', url, get_data, callback, 'GET', true);
            },
            getIncomeList: function(incomeType,callback){
                /**
                 * 获取收入列表
                 */
                var get_data = {incometype:incomeType};
                var url = api_prefix + "/income/get-income-list";
                this.ajax('取汇率比例', url, get_data, callback, 'GET', true);
            },
            ajax: function (title, url, get_data, callback, ajax_type, async, failedCallback) {
                async = typeof async == 'undefined' ? true : async;
                util.myAjax({
                    url: url,
                    type: ajax_type || "GET",
                    context: this,
                    data: get_data,
                    async: async,
                    success: function (json) {
                        if (json.retCode == 0) {
                            if (callback) {
                                callback(json.data || []);
                            }

                        } else {
                            if (failedCallback) {
                                failedCallback();
                            }
                            util.alert("获取" + title + "失败：" + json.errMsg);
                        }
                    },
                    error: function(){
                        if (failedCallback) {
                            failedCallback();
                        }
                    }
                });
            }
        }
        return service;
    }]);

    module.exports = ContractCommon;
});